package org.sdau.rotten.library.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.sdau.rotten.library.DTO.BookExcelData;
import org.sdau.rotten.library.service.BookService;

public class ExcelListener extends AnalysisEventListener<BookExcelData> {

    private BookService bookService;

    public ExcelListener(BookService bookService) {
        this.bookService = bookService;
    }

    @Override
    public void invoke(BookExcelData bookExcelData, AnalysisContext analysisContext) {
        if (bookExcelData == null) {
            throw new RuntimeException("EXCEL不存在");
        } else {
            bookService.addBook(
                    bookExcelData.getBookId(),
                    bookExcelData.getBookName(),
                    bookExcelData.getNumber());
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("导入完成");
    }
}
